<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="!dataForm.id ? '新增' : '修改'"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="分组id" prop="groupBy">
        <el-input v-model="dataForm.groupBy" placeholder="分组id" />
      </el-form-item>
      <el-form-item label="标识" prop="code">
        <el-input v-model="dataForm.code" placeholder="标识" />
      </el-form-item>
      <el-form-item label="显示值" prop="value">
        <el-input v-model="dataForm.value" placeholder="显示值" />
      </el-form-item>
      <el-form-item label="排序" prop="order">
        <el-input v-model="dataForm.order" placeholder="排序" />
      </el-form-item>
      <el-form-item label="有效状态" prop="status">
        <el-input v-model="dataForm.status" placeholder="有效状态" />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="描述" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        groupBy: '',
        code: '',
        value: '',
        order: '',
        status: '',
        description: ''
      },
      dataRule: {
        groupBy: [
          { required: true, message: '分组id不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '标识不能为空', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '显示值不能为空', trigger: 'blur' }
        ],
        order: [
          { required: true, message: '排序不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '有效状态不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/system/dictitem/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.groupBy = data.dictItem.groupBy
              this.dataForm.code = data.dictItem.code
              this.dataForm.value = data.dictItem.value
              this.dataForm.order = data.dictItem.order
              this.dataForm.status = data.dictItem.status
              this.dataForm.description = data.dictItem.description
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/system/dictitem/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'groupBy': this.dataForm.groupBy,
              'code': this.dataForm.code,
              'value': this.dataForm.value,
              'order': this.dataForm.order,
              'status': this.dataForm.status,
              'description': this.dataForm.description
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
